package com.zxl.openapi.api.impl;

import com.zxl.openapi.api.RedisServiceApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class RedisServiceApiImpl implements RedisServiceApi {

    @Autowired
    private StringRedisTemplate template;


    @Override
    public boolean set(String key, String value, long expire) {

        log.error("key:{},value:{}",key,value);

        template.opsForValue().set(key,value);

        return expire(key,expire);
    }

    @Override
    public String get(String key) {

        return template.opsForValue().get(key);

    }

    @Override
    public boolean deleteKey(String key) {

        return template.delete(key);

    }

    @Override
    public boolean expire(String key, long expire) {

        Boolean aBoolean = template.expire(key, expire, TimeUnit.MILLISECONDS);

        return aBoolean;
    }

    @Override
    public Map<Object, Object> hGetAll(String key) {

        Map<Object, Object> entries = template.opsForHash().entries(key);
        return entries;
    }

    @Override
    public Set<String> getSet(String key) {
        return  template.opsForSet().members(key);
    }

    @Override
    public String hget(String key, String field) {
        return (String) template.opsForHash().get(key,field);
    }

    @Override
    public long hdecr(String key, String field, long increment) {
        return template.opsForHash().increment(key, field, increment);
    }

    @Override

    public Set<String> keys(String partten) {
//        template.opsForSet().scan();
        return template.keys(partten);

    }

    @Override
    public void mPut(String key, Map fieldValue) {
        template.opsForHash().putAll(key, fieldValue);
    }
}
